<?php

namespace app\admin\controller;

use app\common\controller\AdminController;

//引入外部函数
use app\common\model\hfztcy\LibMember;
use app\common\model\hfztcy\LibAdminWithdrawalApply;
use app\common\model\LibAdmin;
use app\common\model\LibUser;
use common\Common;


/**
 * 提现记录
 * @name AdminWithdrawalApply.php
 * @package hfztcy
 * @category controller
 * @author rxy
 * @version 1.0
 * @copyright CHANGE INC
 * @since 2020-6-5
 */
class AdminWithdrawalApply extends AdminController
{
    protected $model;
    protected $lib_user;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new LibAdminWithdrawalApply();
        $this->lib_user = new LibUser();
        $this->rightVerify(session('admin'), url("admin/Login/login"));
        parent::__construct();
    }

    /**
     * 代理商-列表页
     */
    function withdrawalList()
    {
        $this->getSetMenu($this);
        $this->assign('state',3);
        $this->log(__CLASS__, __FUNCTION__, "代理商-列表页", 1, 'view');
        return $this->fetch('AdminWithdrawalApply/withdrawalList/withdrawalList');
    }

    /**
     * 客服-列表页
     */
    function withdrawal1List()
    {
        $this->getSetMenu($this);
        $this->assign('state',1);
        $this->log(__CLASS__, __FUNCTION__, "客服部-列表页", 1, 'view');
        return $this->fetch('AdminWithdrawalApply/withdrawal1List/withdrawalList');
    }

    /**
     * 培训-列表页
     */
    function withdrawal2List()
    {
        $this->getSetMenu($this);
        $this->assign('state',2);
        $this->log(__CLASS__, __FUNCTION__, "培训部-列表页", 1, 'view');
        return $this->fetch('AdminWithdrawalApply/withdrawal2List/withdrawalList');
    }

    /**
     * 分页查询
     */
    function pagingWithdrawal()
    {
        $args = $this->getArgsList($this, ['user_id', 'username','state']);
        $page = $this->getPageInfo();
        $where = [];
        $where[] = "`group_id` = {$args['state']}";
        //$where[] = "FIND_IN_SET({$args['state']},identity)";
        if (!empty($args['user_id'])) $where[] = "`user_id` = " . $args['user_id'];
        if (!empty($args['username'])) $where[] = "`account` = '" . $args['username'] . "'";
        $result = $this->model->pagingWithdrawalApply($page, implode(' AND ', $where));
        foreach ($result['data']['dataList'] as &$item) {
            $item['account_info'] = json_decode($item['account_info'],true);
        }
        echo json_encode($result);
    }

    /**
     * Info: 审核提现申请
     * Date: 2022/6/20 11:24
     */
    public function auditStatus()
    {
        $id = input("id");
        $status = input("state");

        $result = $this->model->findWithdrawalApply(['id' => $id]);
        if ($result['errorCode'] == 1) {
            echo json_encode(Common::errorArray(1, '不存在', ''));
            die();
        }
        if ($result['data']['audit_status'] > 0) {
            echo json_encode(Common::errorArray(1, '已审核', ''));
            die();
        }

        $user_id = (int)$result['data']['user_id'];//会员
        $amount = (int)$result['data']['amount'];//提现金额

        $memberModel = new LibAdmin();
        $memberInfo = $memberModel->findAdmin(['id' => $user_id]);
        if ($memberInfo['errorCode'] == 1) {
            echo json_encode(Common::errorArray(1, '用户不存在', ''));
            die();
        }

        $params = [
            'id' => $id,
            'status' => $status,
            'user_id' => $user_id,
            'amount' => $amount,
            'balance' => $memberInfo['data']['balance']
        ];

        $result = $this->model->withdrawalAudit($params);

        echo json_encode($result);
        die();
    }
}

?>